package TUILayer;
import ControlLayer.*;
import ModelLayer.*;
import java.util.ArrayList;
import java.util.Scanner;
/**
 * Write a description of class LoanUI here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class LoanUI
{
    private AddressCtr addrCtr;
    private DVDCtr dvdCtr;
    public LoanUI()
    {
       addrCtr = new AddressCtr();
       dvdCtr = new DVDCtr();
    }
    public void start()
    {
        loanMenu();
    }
    private void loanMenu()  
      {
          boolean exit=false;
        
          while(!exit)
          {
            int choice = writeLoanMenu();
            if(choice == 1)
            {
                loanDVD();
            }
            else
            {
                if(choice == 2)
                {
                    returnedDVD();
                }
                else{
                        exit=true;
                    }
                }
            }
      }
    
    private void pause()
    {
         Scanner keyboard = new Scanner(System.in);
         System.out.println("Enter to continue");
         String address = keyboard.nextLine();
    }    
    private int writeLoanMenu()
       {
        Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** AddressBook Menu ***");
            System.out.println(" (1) Loan DVD");
            System.out.println(" (2) Return DVD");
            System.out.println(" (3) Return");
            System.out.print("\n Make your choice: ");
            
            int choice = keyboard.nextInt();
            return choice;
        }
        private int inputContinue()
        {
            Scanner keyboard = new Scanner(System.in);
            int choice = keyboard.nextInt();
            return choice;
        }
    private long inputID()
    {
        boolean ok = false;
        long id = 0;
        while(!ok)
        {
        // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           try{
             id = keyboard.nextLong();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("You must enter a number -  try again");
             String input = keyboard.nextLine();
           }
        }//end while
        return id;
    }
    private int inputDVD()
    {boolean ok = false;
        int id = 0;
        while(!ok)
        {
        // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Write Id:  ");
           try{
             id = keyboard.nextInt();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("You must enter a number -  try again");
             String input = keyboard.nextLine();
           }
        }//end while
        return id;
    }
    private String inputPurchaseDate()
    {
        Scanner keyboard = new Scanner(System.in);
        
        String no = keyboard.nextLine();
        return no;
    }
    private void loanDVD()
    {
        
        System.out.println("Please insert the customers id");
        long search = inputID();
        int a = addrCtr.check(search);
        if(a==1)
        {
            System.out.println("There is no such person");
        }
        else{
            if(a==2)
            {
                System.out.println("The person already has a loan");
            }
            else
            {
                System.out.println("The person may rent");
                
                System.out.println("What is the id of the DVD?");
                int cauta = inputDVD();
                Copy p = dvdCtr.checkID(cauta);
                if(p!=null)
                {
                    int serial = p.getSerialNumber();
                    String purchase = p.getPurchaseDate();
                    double pret = p.getPurchasePrice();
                    System.out.println("The following copy is available");
                    System.out.println("\n"+serial+" "+purchase+" "+pret);
                    System.out.println("\n Do you wish to continue?if so insert 1");
                    int choice = inputContinue();
                    if(choice==1)
                    {
                        System.out.println("Thank you for your purchase, please insert the loan information");
                        System.out.println("\n Loan id:");
                        long loanID = inputID();
                        System.out.println("\n Current Date:");
                        String loanDate = inputPurchaseDate();
                        System.out.println("\n Rent period:");
                        String loanPeriod = inputPurchaseDate();
                        
                        addrCtr.addLoan(search, loanID, loanDate, loanPeriod);
                        dvdCtr.rentCopy(cauta, loanID, loanDate, loanPeriod);
                        
                    }
                }
            }
        }
        pause();
        
    }
    private void returnedDVD()
    {
        System.out.println("Please insert the customers id");
        long search = inputID();
        Person p = addrCtr.getPerson(search);
        System.out.println("Insert the movie id");
        int die = inputDVD();
        int tired = dvdCtr.findDVD(die);
        System.out.println("Please insert copy id");
        int searchCopy = inputContinue();
        Copy solution = dvdCtr.noIdea(die, searchCopy);
        if(p!=null)
        {
            if(tired!=0)
            {
                if(solution!=null)
                {
            System.out.println("Is it overdue?, press 1 if yes.");
            int choice = inputContinue();
            p.removeLoan(choice);
            
            System.out.println("The loan has been removed");
        }
            else{
                   System.out.println("the copy is in another castle");
                }
        }
        }
        else
        {
            System.out.println("Person is inexistent....and an asshole");
        }
    }
}



